package org.valire.db;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

public class SqlExecutorFactory {

	public static SqlExecutor fromConfig(JdbcInfoConfig config) throws Exception{
		JdbcConnectionInfo info = config.getJdbcConnectionInfo();
		DataSource dataSource = DataSourceFactory.createDataSource(info);
		DataSourceTransactionManager txManager = TxManagerFactory.createDsTxManager(dataSource);
		SqlExecutor sqlExecutor = new SqlExecutor();
		sqlExecutor.setJdbcTemplate(new JdbcTemplate(dataSource));
		sqlExecutor.setTransactionManager(txManager);
		return sqlExecutor;
	}
}
